<?php
namespace app\common\model\mysql;
use think\Model;

class User extends Model {
    public function getAllUserList($limit){

        $result =  $this->limit($limit)->select()->toArray();

        return $result;
    }


    public function getUserByPhoneNumber($phoneNumber){
        if(empty($phoneNumber)){
            return false;
        }
        $result = $this->where([
            "phone_number" => $phoneNumber,
        ])->find();
        return $result;
    }

    /**
     * 根据ID修改user
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateById($id,$data){
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        $res = $this->where(
            [
                "id" => $id
            ]
        )->save($data);
        return $res;
    }
    public function updateByPhoneNumber($phoneNumber,$data){
        $res = $this->where(
            [
                "phone_number" => $phoneNumber
            ]
        )->save($data);
        return $res;
    }

    /**
     * 根据Id获取用户信息
     * @param $id
     * @return array|bool|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserById($id){
        $id = intval($id);
        if(!$id){
            return false;
        }
        return $this->find($id);
    }

    /**根据用户名获取用户信息
     * @param $username
     * @return array|bool|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserByUsername($username){
        if(empty($username)){
            return false;
        }
        $result = $this->where([
            "phone_number" => $username,
        ])->find();
        return $result;
    }
}